﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DownloadPage.aspx.cs" Inherits="NPSoft.Ferrero.Framework.Web.Test.DownloadPage" %>

<!DOCTYPE html>
<script language="C#" runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        this.download3();
    }
    public void download()
    {
        string filePath = "C:\\Newspage\\Interface\\In\\Employee\\";
        string fileName = "MST_EMPLOYEE.sql";
        //以字符流的形式下载文件
        System.IO.FileStream fs = new System.IO.FileStream("https://ferreroapp.chinacloudapp.cn/Ferrero China Reports/02 EX-Distributor/01 National Reports/04 EX-D By SKU.xlsx", System.IO.FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();
        Response.ContentType = "application/octet-stream";
        //通知浏览器下载文件而不是打开
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("1.xlsx", System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }

    public void download2()
    {
        string uri = "https://ferreroapp.chinacloudapp.cn/Ferrero China Reports/02 EX-Distributor/01 National Reports/04 EX-D By SKU.xlsx";
        System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
        req.Credentials = new System.Net.NetworkCredential("sp_farm", "Abcd@1234!", "ferreroazure");
        req.Method = "GET";
        using (System.Net.WebResponse response = req.GetResponse())
        {
            byte[] bytes = new byte[128];
            using (System.IO.Stream stream = response.GetResponseStream())
            {
                System.IO.Stream outStream = req.GetRequestStream();
                bytes = new byte[(int)outStream.Length];
                outStream.Read(bytes, 0, bytes.Length);
                outStream.Close();
               
            }
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("1.xlsx", System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
    }

    public void download3()
    {
        string uri = "https://ferreroapp.chinacloudapp.cn/Ferrero China Reports/02 EX-Distributor/01 National Reports/04 EX-D By SKU.xlsx";
        System.Net.WebClient wcClient = new System.Net.WebClient();
        //对远程文件发送一个请求
        System.Net.WebRequest webReq = System.Net.WebRequest.Create(uri);
        webReq.Credentials = new System.Net.NetworkCredential("sp_farm", "Abcd@1234!", "ferreroazure");
        //接收远程WEB服务器发回的响应
        System.Net.WebResponse webRes = webReq.GetResponse();
        //获取文件长度
        long fileLength = webRes.ContentLength;
        System.IO.Stream srm = webRes.GetResponseStream();
        //使用特殊编码读取流信息
        System.IO.StreamReader srmReader = new System.IO.StreamReader(srm);
        //定义缓冲区
        byte[] bytes = new byte[fileLength];
        srm.Read(bytes, 0, bytes.Length);
       
        srm.Close();
        srmReader.Close();

        Response.ContentType = "application/octet-stream";
        //通知浏览器下载文件而不是打开
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("1.xlsx", System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>
